package res

import (
	"gitee.com/daofeixian/dfx_golang_toolset/utils/validate"
	"github.com/gin-gonic/gin"
)

type Res struct {
	Code int64  `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

func response(code int64, data any, msg string, c *gin.Context) {
	r := Res{
		Code: code,
		Msg:  msg,
		Data: data,
	}
	c.JSON(200, r)
}

func OkWithData(data any, c *gin.Context) {
	response(0, data, "ok", c)
}

func OkWithMsg(msg string, c *gin.Context) {
	response(0, gin.H{}, msg, c)
}

func OkWithList(list any, count int64, c *gin.Context) {
	response(0, gin.H{
		"list":  list,
		"count": count,
	}, "ok", c)
}

func OK(data any, msg string, c *gin.Context) {
	response(0, data, msg, c)
}

func FailWithMsg(msg string, c *gin.Context) {
	response(1001, gin.H{}, msg, c)
}

func Fail(data any, msg string, c *gin.Context) {
	response(1001, data, msg, c)
}

func FailWithError(err error, c *gin.Context) {
	response(1001, gin.H{}, err.Error(), c)
}

func FailWithBindingError(err error, c *gin.Context) {
	res := validate.ValidateErr(err)
	response(1001, res.FieldMap, res.Msg, c)
}
